package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.issue.IssueCreateBO;
import com.cskaoyan.bean.issue.IssueCreateVO;
import com.cskaoyan.bean.issue.IssueListUnitVO;
import com.cskaoyan.bean.issue.IssueReq;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.util.CskaoyanPageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.System;

import java.lang.System;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: whyccz-mall-project
 * @description:
 * @create: 2021-06-06 20:48
 **/
@Service
public class IssueServiceImpl implements IssueService {
	@Autowired
	IssueMapper issueMapper;
	@Override
	public BaseData<IssueListUnitVO> selectIssueList(IssueReq issueReq) {
		CskaoyanPageHelper.startPage(issueReq);//调用分页插件


		//每个service方法创建一个example对象
		IssueExample issueExample = new IssueExample();
		//排序信息
		issueExample.setOrderByClause(issueReq.getSort()+" "+issueReq.getOrder());
		IssueExample.Criteria criteria = issueExample.createCriteria();
		if (issueReq.getQuestion() !=null&& !"".equals(issueReq.getQuestion())){
			criteria.andQuestionLike("%" + issueReq.getQuestion() + "%");
		}

		criteria.andDeletedEqualTo(false);

		List<Issue> issues = issueMapper.selectByExample(issueExample);

		//获得分页信息
		PageInfo<Issue> pageInfo = new PageInfo<>(issues);
		long total = pageInfo.getTotal();

		return BaseData.ok(issues,total);
	}

	@Override
	public IssueCreateVO createIssue(IssueCreateBO issueCreateBO) {

		Issue issue = new Issue();
		issue.setQuestion(issueCreateBO.getQuestion());
		issue.setAnswer(issueCreateBO.getAnswer());
		issue.setAddTime(new Timestamp(System.currentTimeMillis()));
		issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		issue.setDeleted(false);
		issueMapper.insertSelective(issue);
		Integer id = issue.getId();

		IssueCreateVO issueCreateVO = new IssueCreateVO();
		issueCreateVO.setId(id);
		issueCreateVO.setQuestion(issueCreateBO.getQuestion());
		issueCreateVO.setAnswer(issueCreateBO.getAnswer());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(new Date());
		issueCreateVO.setAddTime(format);
		issueCreateVO.setUpdateTime(format);
		return issueCreateVO;
	}

	@Override
	public IssueCreateVO updateIssue(IssueCreateVO issueCreateVO) {
		Issue issue = new Issue();
		issue.setId(issueCreateVO.getId());
		issue.setQuestion(issueCreateVO.getQuestion());
		issue.setAnswer(issueCreateVO.getAnswer());
		issue.setAddTime(Timestamp.valueOf(issueCreateVO.getAddTime()));
		issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		issue.setDeleted(false);
		issueMapper.updateByPrimaryKeySelective(issue);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(new Date());
		issueCreateVO.setUpdateTime(format);

		return issueCreateVO;
	}

	@Override
	public void deleteIssue(IssueCreateVO issueCreateVO) {
		issueMapper.deleteById(issueCreateVO.getId());
	}


}
